import 'package:flutter/material.dart';

class TabControllerPage extends StatefulWidget {
  TabControllerPage({Key? key}) : super(key: key);

  @override
  State<TabControllerPage> createState() => _TabControllerPageState();
}

class _TabControllerPageState extends State<TabControllerPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void dispose() {
    // TODO: implement dispose生命周期函数-组件销毁
    super.dispose();
    _tabController.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState生命周期函数-初始化
    super.initState();
    _tabController = new TabController(length: 2, vsync: this);
    _tabController.addListener(() {
      print(_tabController.index); //监听
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("TabControllerPage"),
        bottom: TabBar(
          controller: this._tabController,
          tabs: <Widget>[
            Tab(
              text: "热销",
            ),
            Tab(
              text: "推荐",
            ),
          ],
        ),
      ),
      body: TabBarView(controller: this._tabController, children: <Widget>[
        Center(
          child: Text("热销"),
        ),
        Center(
          child: Text("推荐"),
        ),
      ]),
    );
  }
}
